/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.teaching.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.exception.TrainingServiceException;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.course.entity.CourseChapterSubEntity;
import org.springblade.modules.course.service.ICourseService;
import org.springblade.modules.course.vo.CourseChapterSubVO;
import org.springblade.modules.library.entity.MaterialLibraryEntity;
import org.springblade.modules.library.service.IMaterialLibraryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.teaching.entity.CourseChapterSubFileEntity;
import org.springblade.modules.teaching.vo.CourseChapterSubFileVO;
import org.springblade.modules.teaching.wrapper.CourseChapterSubFileWrapper;
import org.springblade.modules.teaching.service.ICourseChapterSubFileService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Date;

/**
 * 课程小节与资料文件关联表 控制器
 *
 * @author junxin
 * @since 2023-03-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-courseChapterSubFile/courseChapterSubFile")
@Api(value = "课程小节与资料文件关联表", tags = "课程小节与资料文件关联表接口")
public class CourseChapterSubFileController extends BladeController {

	private final ICourseChapterSubFileService courseChapterSubFileService;
	private final ICourseService courseService;
	private final CommonUtil commonUtil;
	private final IMaterialLibraryService materialLibraryService;
	/**
	 * 课程小节与资料文件关联表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入courseChapterSubFile")
	public R<CourseChapterSubFileVO> detail(CourseChapterSubFileEntity courseChapterSubFile) {
		CourseChapterSubFileEntity detail = courseChapterSubFileService.getOne(Condition.getQueryWrapper(courseChapterSubFile));
		return R.data(CourseChapterSubFileWrapper.build().entityVO(detail));
	}

	/**
	 * 课程小节与资源文件关联表 自定义分页
	 * 老师就只能看到自己的，管理员可以看到全部的
	 * 教学视频列表attachType=2;学习课件列表attachType=3
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入courseChapterSubFile")
	public R<IPage<CourseChapterSubFileVO>> page(CourseChapterSubFileVO courseChapterSubFile, Query query) {
//		if (!commonUtil.checkRoleAlias(TrainingRoleConstant.Administrator)) courseChapterSubFile.setCreateUser(AuthUtil.getUserId());
		commonUtil.checkRoleAliasPermission(courseChapterSubFile,courseChapterSubFile.getClass());
		IPage<CourseChapterSubFileVO> pages = courseChapterSubFileService.selectCourseChapterSubFilePage(Condition.getPage(query), courseChapterSubFile);
		return R.data(pages);
	}

	/**
	 * 课程小节与资料文件关联表 新增
	 * 教学视频新增的时候，attachType=2;学习课件新增的时候 attachType=3
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入courseChapterSubFile")
	@Transactional(rollbackFor = Exception.class)
	public R save(@Valid @RequestBody CourseChapterSubFileVO courseChapterSubFile) {
		if (null==courseChapterSubFile.getCoursewareCourse()) throw new TrainingServiceException("请选择是否课件课程");
		MaterialLibraryEntity materialLibraryEntity = materialLibraryService.getById(courseChapterSubFile.getConnectId());
		if (null!=materialLibraryEntity) {
			courseChapterSubFile.setLink(materialLibraryEntity.getStoragePath());
			courseChapterSubFile.setLinkName(materialLibraryEntity.getMeterialName());
		}
		//如果非课件课程,则connectId不能为空
		if (0==courseChapterSubFile.getCoursewareCourse() && null==courseChapterSubFile.getConnectId()) throw new TrainingServiceException("必须选择课程、章节和小节");
		if (1== courseChapterSubFile.getCoursewareCourse()){//如果为课件课程，则默认要创建课程、章节、小节
			CourseChapterSubEntity courseChapterSubEntity = courseService.createCourseAndChapterAndSub(courseChapterSubFile.getCoursewareCourseName());
			courseChapterSubFile.setSubId(courseChapterSubEntity.getId());
		}
		return R.status(courseChapterSubFileService.save(courseChapterSubFile));
	}

	/**
	 * 课程小节与资料文件关联表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入courseChapterSubFile")
	public R update(@Valid @RequestBody CourseChapterSubFileEntity courseChapterSubFile) {
		MaterialLibraryEntity materialLibraryEntity = materialLibraryService.getById(courseChapterSubFile.getConnectId());
		if (null!=materialLibraryEntity) {
			courseChapterSubFile.setLink(materialLibraryEntity.getStoragePath());
			courseChapterSubFile.setLinkName(materialLibraryEntity.getMeterialName());
		}
		return R.status(courseChapterSubFileService.updateById(courseChapterSubFile));
	}

	/**
	 * 课程小节与资料文件关联表 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(courseChapterSubFileService.deleteLogic(Func.toLongList(ids)));
	}

	public static void main(String[] args) {
		long tKey=System.currentTimeMillis()/1000;
		System.out.println(tKey);
		System.out.println(Func.md5Hex("16f65fa116362f316132d19954946f3f"+tKey));
	}
}
